package com.chenyi.web.controller.system;

import com.chenyi.common.annotation.Log;
import com.chenyi.common.constant.UserConstants;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.entity.SysAdmin;
import com.chenyi.common.core.page.TableDataInfo;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.exception.MessageException;
import com.chenyi.common.utils.AdminUtils;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.system.domain.SysAdminPost;
import com.chenyi.system.service.ISysAdminPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author wangkh
 */
@RestController
@RequestMapping("/admin/post")
public class AdminPostController extends BaseController
{
    @Autowired
    private ISysAdminPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@admin.hasPermi('system:post:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysAdminPost post)
    {
        startPage();
        List<SysAdminPost> list = postService.selectAdminPostList(post);
        return new ResponseEntity<>(getDataTable(list),HttpStatus.OK);
    }
    
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@admin.hasPermi('system:post:export')")
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysAdminPost post)
    {
        List<SysAdminPost> list = postService.selectAdminPostList(post);
        ExcelUtil<SysAdminPost> util = new ExcelUtil<SysAdminPost>(SysAdminPost.class);
        return util.exportExcel(list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@admin.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public ResponseEntity<Object> getInfo(@PathVariable Long postId)
    {
        return ResponseEntity.ok(postService.selectAdminPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@admin.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Object> add(@Validated @RequestBody SysAdminPost post)
    {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkAdminPostNameUnique(post)))
        {
            throw new MessageException("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkAdminPostCodeUnique(post)))
        {
            throw new MessageException("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(AdminUtils.getUsername());
        postService.insertAdminPost(post);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@admin.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Object> edit(@Validated @RequestBody SysAdminPost post)
    {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkAdminPostNameUnique(post)))
        {
            throw new MessageException("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkAdminPostCodeUnique(post)))
        {
            throw new MessageException("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(AdminUtils.getUsername());
        postService.updateAdminPost(post);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@admin.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] postIds)
    {
        postService.deleteAdminPostByIds(postIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public ResponseEntity<Object> optionselect()
    {
        List<SysAdminPost> posts = postService.selectAdminPostAll();
        return ResponseEntity.ok(posts);
    }
}
